package com.bidevalution.business.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.bidevalution.business.service.BeSectionInfoService;
import com.bidevalution.business.service.BeTechnologyItemService;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.support.util.MapUtil;
import com.bidevalution.support.util.StringUtil;
import com.google.common.collect.Lists;

@Service
public class BeTechnologyItemServiceImpl implements BeTechnologyItemService {
	@Autowired
	private JdbcTemplateService jdbcTemplateService;
	@Autowired
	private BeSectionInfoService beSectionInfoService;
	/**
	 * techScoreHyper:细项分上限
	 * techScoreUnder:细项分下限
	 * techScoreLineHyper:最高分上限
	 * techScoreLineUnder:最低分下限
	 * techScoreFirHyper:细项分一类上限
	 * techScoreFirUnder:细项分一类下限
	 * techScoreSecHyper:细项分二类上限
	 * techScoreSecUnder:细项分二类下限
	 * techScoreThiHyper:细项分三类上限
	 * techScoreThiUnder:细项分三类下限
	 * techScoreFouHyper:细项分四类上限
	 * techScoreFouUnder:细项分四类下限
	 * techScoreDefault:缺省项分
	 * <p>Description: </p>
	 * @param sectionId
	 * @param parentId
	 * @param type
	 * @return
	 * @see com.bidevalution.business.service.BeTechnologyItemService#selectTree(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> selectTree(String sectionId, String parentId, String type) {
		List<Map<String, Object>> returnList = Lists.newArrayList();
		List<Map<String, Object>> childList = Lists.newArrayList();
		BeSectionInfo beSectionInfo = beSectionInfoService.getBeSectionInfoById(sectionId);
		List<Map<String, Object>> dataList = jdbcTemplateService.selectForList(
				"select tech_JSMethodID,tech_Name,tech_Idx,tech_ParentID,tech_ProjectID,tech_ScoreHyper,tech_ScoreUnder,tech_ScoreLineHyper,tech_ScoreLineUnder,tech_ScoreFirHyper,tech_ScoreFirUnder,tech_ScoreSecHyper,tech_ScoreSecUnder,tech_ScoreThiHyper,tech_ScoreThiUnder,tech_ScoreFouHyper,tech_ScoreFouUnder,tech_ScoreDefault from td_Technology where tech_ProjectID=:projectId and tech_ParentID=:parentId "
				+ (type==null?"":" and tech_Type=:type")
				+ " order by tech_Idx asc",
				MapUtil.create().putValue("projectId", beSectionInfo.getTdProjectid())
				.putValue("parentId", parentId)
				.putValue("type", type)
				.getMap());
		for (Map<String, Object> dataMap : dataList) {
			childList.add(MapUtil.create().putValue("id", dataMap.get("tech_JSMethodID"))
					.putValue("nodeNumber", dataMap.get("tech_Idx")).putValue("nodeName", dataMap.get("tech_Name"))
					.putValue("techScoreHyper", dataMap.get("tech_ScoreHyper"))
					.putValue("techScoreUnder", dataMap.get("tech_ScoreUnder"))
					.putValue("techScoreLineHyper", dataMap.get("tech_ScoreLineHyper"))
					.putValue("techScoreLineUnder", dataMap.get("tech_ScoreLineUnder"))
					.putValue("techScoreFirHyper", dataMap.get("tech_ScoreFirHyper"))
					.putValue("techScoreFirUnder", dataMap.get("tech_ScoreFirUnder"))
					.putValue("techScoreSecHyper", dataMap.get("tech_ScoreSecHyper"))
					.putValue("techScoreSecUnder", dataMap.get("tech_ScoreSecUnder"))
					.putValue("techScoreThiHyper", dataMap.get("tech_ScoreThiHyper"))
					.putValue("techScoreThiUnder", dataMap.get("tech_ScoreThiUnder"))
					.putValue("techScoreFouHyper", dataMap.get("tech_ScoreFouHyper"))
					.putValue("techScoreFouUnder", dataMap.get("tech_ScoreFouUnder"))
					.putValue("techScoreDefault", dataMap.get("tech_ScoreDefault"))
					.putValue("children",
							getChildrenNode(beSectionInfo.getTdProjectid(), StringUtil.toString(dataMap.get("tech_JSMethodID")), type))
					.getMap());
		}

		returnList.add(MapUtil.create().putValue("children", childList).putValue("id", parentId)
				.putValue("nodeName", TechType.geTechType(type)==null?"技术标评审项目":TechType.geTechType(type).getText()).getMap());

		return returnList;
	}

	/**
	 * 递归算法解析成树形结构
	 *
	 * @param defineId
	 * @param seqId
	 * @return
	 * @author daizhifeng
	 */
	public List<Map<String, Object>> getChildrenNode(String projectId, String seqId, String type) {
		List<Map<String, Object>> returnList = Lists.newArrayList();
		// 遍历子节点
		for (Map<String, Object> dataMap : jdbcTemplateService.selectForList(
				"select tech_JSMethodID,tech_Name,tech_Idx,tech_ParentID,tech_ProjectID,tech_ScoreHyper,tech_ScoreUnder,tech_ScoreLineHyper,tech_ScoreLineUnder,tech_ScoreFirHyper,tech_ScoreFirUnder,tech_ScoreSecHyper,tech_ScoreSecUnder,tech_ScoreThiHyper,tech_ScoreThiUnder,tech_ScoreFouHyper,tech_ScoreFouUnder,tech_ScoreDefault from td_Technology where tech_ProjectID=:projectId and tech_ParentID=:parentId "
				+ (type==null?"":" and tech_Type=:type ")
				+ "order by tech_Idx asc",
				MapUtil.create().putValue("projectId", projectId).putValue("parentId", seqId).putValue("type", type)
						.getMap())) {
			if (!StringUtils.isEmpty(dataMap.get("tech_JSMethodID"))) {
				returnList.add(MapUtil.create().putValue("id", dataMap.get("tech_Idx"))
						.putValue("nodeNumber", dataMap.get("tech_Idx")).putValue("nodeName", dataMap.get("tech_Name"))
						.putValue("techScoreHyper", dataMap.get("tech_ScoreHyper"))
						.putValue("techScoreUnder", dataMap.get("tech_ScoreUnder"))
						.putValue("techScoreLineHyper", dataMap.get("tech_ScoreLineHyper"))
						.putValue("techScoreLineUnder", dataMap.get("tech_ScoreLineUnder"))
						.putValue("techScoreFirHyper", dataMap.get("tech_ScoreFirHyper"))
						.putValue("techScoreFirUnder", dataMap.get("tech_ScoreFirUnder"))
						.putValue("techScoreSecHyper", dataMap.get("tech_ScoreSecHyper"))
						.putValue("techScoreSecUnder", dataMap.get("tech_ScoreSecUnder"))
						.putValue("techScoreThiHyper", dataMap.get("tech_ScoreThiHyper"))
						.putValue("techScoreThiUnder", dataMap.get("tech_ScoreThiUnder"))
						.putValue("techScoreFouHyper", dataMap.get("tech_ScoreFouHyper"))
						.putValue("techScoreFouUnder", dataMap.get("tech_ScoreFouUnder"))
						.putValue("techScoreDefault", dataMap.get("tech_ScoreDefault"))
						.putValue("children",
								getChildrenNode(projectId, StringUtil.toString(dataMap.get("tech_JSMethodID")), type))
						.getMap());
			}

		}
		return returnList;
	}

	enum TechType {
		Tech0("0", "通过型通过制"), Tech1("1", "打分型通过制（总分平均）"), Tech2("2", "打分型通过制（细项平均)"), Tech3("3", "细项平均打分制"),
		Tech4("4", "总分平均打分制");
		private String type;
		private String text;

		private TechType(String type, String text) {
			this.type = type;
			this.text = text;
		}

		public String getType() {
			return type;
		}

		public String getText() {
			return text;
		}
		public static TechType geTechType(String type) {
			for(TechType techType:TechType.values()) {
				if(techType.getType().equals(type)) {
					return techType;
				}
			}
			return null;
		}
	}
}
